package com.collect.zambia.zhiScene.zhiCollection.zhiGroup.zhiPerformance.zhiB1;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.collect.zambia.R;
import com.collect.zambia.zhiAdater.zhiLevelDetail.ZHILevelDetailCollectGrouperB1;
import com.collect.zambia.zhiBase.BaseFragment;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRCollectGrouperPerformanceListEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRCollectMemberPerformanceListEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRCollecterPerformanceEntity;
import com.collect.zambia.zhiBean.zhiSend.ZHISPerfermanceEntity;
import com.collect.zambia.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.zambia.zhiHelper.ZHIMainProcessHelper;
import com.collect.zambia.zhiScene.zhiCollection.zhiGroup.zhiPerformance.zhiSort.ZHICollectGrouperPerformanceCompare;
import com.collect.zambia.zhiScene.zhiWidget.zhiDialog.ZHIDialogCollectGrouperB1Performance;
import com.collect.zambia.zhiScene.zhiWidget.zhiDialog.ZHIDialogCollectMemberPerformance;
import com.collect.zambia.zhiScene.zhiWidget.zhiDialog.ZHIDialogCustomerPerformanceAdminBonusV2;
import com.collect.zambia.zhiScene.zhiWidget.zhiDialog.ZHIDialogMessage;
import com.collect.zambia.zhiUtils.TextViewUtil;
import com.collect.zambia.zhiUtils.ZHICalendarUtil;

import java.util.ArrayList;
import java.util.Collections;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
public class ZHIPerformanceGroupB1Fragment extends BaseFragment<ZHIPerformanceGroupB1Presenter> implements ZHIPerformanceGroupB1Contract.View {

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    @BindView(R.id.tv_page_title)
    TextView tv_page_title;

    @BindView(R.id.tv_cumulative_bonus)
    TextView tv_cumulative_bonus;

    @BindView(R.id.tv_cumulative_bonus_date)
    TextView tv_cumulative_bonus_date;
    @BindView(R.id.iv_cumulative_bonus_date)
    ImageView iv_cumulative_bonus_date;

    @BindView(R.id.tv_real_time_data)
    TextView tv_real_time_data;
    @BindView(R.id.iv_real_time_data)
    ImageView iv_real_time_data;

    @BindView(R.id.ll_total_case_num)
    LinearLayout ll_total_case_num;
    @BindView(R.id.ll_total_task_num)
    LinearLayout ll_total_task_num;
    @BindView(R.id.ll_total_settled_num)
    LinearLayout ll_total_settled_num;
    @BindView(R.id.ll_total_bouns)
    LinearLayout ll_total_bouns;

    @BindView(R.id.tv_total_case_num)
    TextView tv_total_case_num;
    @BindView(R.id.tv_total_task_num)
    TextView tv_total_task_num;
    @BindView(R.id.tv_total_settled_num)
    TextView tv_total_settled_num;
    @BindView(R.id.tv_total_bouns)
    TextView tv_total_bouns;

    @BindView(R.id.iv_note)
    ImageView iv_note;
    @BindView(R.id.iv_filter)
    ImageView iv_filter;
    @BindView(R.id.tv_fca_old_version)
    TextView tv_fca_old_version;

    @BindView(R.id.rc_detail_list)
    RecyclerView rc_detail_list;

    @BindView(R.id.ll_detail_title_bg)
    LinearLayout ll_detail_title_bg;
    @BindView(R.id.tv_detail_click)
    TextView tv_detail_click;
    @BindView(R.id.iv_sort)
    ImageView iv_sort;

    @BindView(R.id.ll_dis_date_bg)
    LinearLayout ll_dis_date_bg;

    @BindView(R.id.ll_user_bg)
    LinearLayout ll_user_bg;

    @BindView(R.id.ll_num_bg)
    LinearLayout ll_num_bg;

    @BindView(R.id.ll_settle_num_bg)
    LinearLayout ll_settle_num_bg;

    @BindView(R.id.ll_collected_rate_bg)
    LinearLayout ll_collected_rate_bg;

    private boolean mIsSortDisDateDes = true;
    private boolean mIsSortRepayDateDes = false;
    private boolean mIsSortNumDes = false;
    private boolean mIsSortSettleNumDes = false;
    private boolean mIsSortCollectedRateDes = false;

    private boolean mIsSortCollectedRateDes2 = false;

    private final ZHILevelDetailCollectGrouperB1 mZHILevelDetailCollectGrouperB1 = new ZHILevelDetailCollectGrouperB1();

    private String mTotalBonus = "";
    private String mTotalPenalty = "";
    private String mTotalActualBonus = "";

    private ArrayList<ZHIRCollecterPerformanceEntity> mPerformanceEntityArrayList;
    private ArrayList<ZHIRCollecterPerformanceEntity> mLeaderDataList;

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_perfermance_group_b1;
    }

    @Override
    protected void initView(View view) {
        try {
            ZHICalendarUtil.initDateTimeOf0706L();
            ZHICalendarUtil.initDateTimeOf0801L();

            srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));

            tv_page_title.setOnClickListener(v -> {
            });

            iv_note.setOnClickListener(v -> ZHIProjectRouter.toPerformanceCustomerBonusRules());
            tv_fca_old_version.setOnClickListener(v -> ZHIProjectRouter.toPerformanceGroupOrderOld());

            ll_total_task_num.setOnClickListener(v -> ZHIDialogCollectGrouperB1Performance.showDataSource(getActivity(), ZHIMainProcessHelper.getInstance().getFullName(), mLeaderDataList));
            ll_total_case_num.setOnClickListener(v -> ZHIDialogCollectGrouperB1Performance.showDataSource(getActivity(), ZHIMainProcessHelper.getInstance().getFullName(), mLeaderDataList));
            ll_total_settled_num.setOnClickListener(v -> ZHIDialogCollectGrouperB1Performance.showDataSource(getActivity(), ZHIMainProcessHelper.getInstance().getFullName(), mLeaderDataList));
            ll_total_bouns.setOnClickListener(v -> ZHIDialogCustomerPerformanceAdminBonusV2.showDataSource(getActivity(), ZHIMainProcessHelper.getInstance().getFullName(), mTotalBonus, mTotalPenalty, mTotalActualBonus));

            iv_cumulative_bonus_date.setOnClickListener(v -> ZHIProjectRouter.toRootGroupB1PerformanceHistory());
            tv_cumulative_bonus_date.setOnClickListener(v -> ZHIProjectRouter.toRootGroupB1PerformanceHistory());

            tv_real_time_data.setOnClickListener(v -> ZHIProjectRouter.toPerformanceGroupRealTime());
            iv_real_time_data.setOnClickListener(v -> ZHIProjectRouter.toPerformanceGroupRealTime());

            iv_sort.setOnClickListener(v -> {
//                try {
//                    if (mPerformanceEntityArrayList == null) {
//                        return;
//                    }
//
//                    if (mIsSortCollectedRateDes) {
//                        mIsSortCollectedRateDes = false;
//                        Collections.sort(mPerformanceEntityArrayList, new ZHICollectGrouperPerformanceCompare.ZHICollectGrouperPerformanceCompareBySettleRateASC());
//                    } else {
//                        mIsSortCollectedRateDes = true;
//                        Collections.sort(mPerformanceEntityArrayList, new ZHICollectGrouperPerformanceCompare.ZHICollectGrouperPerformanceCompareBySettleRateDESC());
//                    }
//
//                    mZHILevelDetailCollectGrouperB1.notifyDataSetChanged();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
            });

            ll_dis_date_bg.setOnClickListener(v -> {
//                try {
//                    if (mPerformanceEntityArrayList == null) {
//                        return;
//                    }
//
//                    if (mIsSortDisDateDes) {
//                        mIsSortDisDateDes = false;
//                        Collections.sort(mPerformanceEntityArrayList, new ZHICollectGrouperPerformanceCompare.ZHICollectGrouperPerformanceCompareByAssignDateASC());
//                    } else {
//                        mIsSortDisDateDes = true;
//                        Collections.sort(mPerformanceEntityArrayList, new ZHICollectGrouperPerformanceCompare.ZHICollectGrouperPerformanceCompareByAssignDateDESC());
//                    }
//
//                    mZHILevelDetailCollectGrouperB1.notifyDataSetChanged();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
            });

            ll_user_bg.setOnClickListener(v -> {
//                try {
//                    if (mPerformanceEntityArrayList == null) {
//                        return;
//                    }
//
//                    if (mIsSortRepayDateDes) {
//                        mIsSortRepayDateDes = false;
//                        Collections.sort(mPerformanceEntityArrayList, new ZHICollectGrouperPerformanceCompare.ZHICollectGrouperPerformanceCompareByRepayDateASC());
//                    } else {
//                        mIsSortRepayDateDes = true;
//                        Collections.sort(mPerformanceEntityArrayList, new ZHICollectGrouperPerformanceCompare.ZHICollectGrouperPerformanceCompareByRepayDateDESC());
//                    }
//
//                    mZHILevelDetailCollectGrouperB1.notifyDataSetChanged();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
            });

            ll_num_bg.setOnClickListener(v -> {
//                try {
//                    if (mPerformanceEntityArrayList == null) {
//                        return;
//                    }
//
//                    if (mIsSortNumDes) {
//                        mIsSortNumDes = false;
//                        Collections.sort(mPerformanceEntityArrayList, new ZHICollectGrouperPerformanceCompare.ZHICollectGrouperPerformanceCompareByAssignNumASC());
//                    } else {
//                        mIsSortNumDes = true;
//                        Collections.sort(mPerformanceEntityArrayList, new ZHICollectGrouperPerformanceCompare.ZHICollectGrouperPerformanceCompareByAssignNumDESC());
//                    }
//
//                    mZHILevelDetailCollectGrouperB1.notifyDataSetChanged();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
            });

            ll_settle_num_bg.setOnClickListener(v -> {
//                try {
//                    if (mPerformanceEntityArrayList == null) {
//                        return;
//                    }
//
//                    if (mIsSortSettleNumDes) {
//                        mIsSortSettleNumDes = false;
//                        Collections.sort(mPerformanceEntityArrayList, new ZHICollectGrouperPerformanceCompare.ZHICollectGrouperPerformanceCompareBySettleNumASC());
//                    } else {
//                        mIsSortSettleNumDes = true;
//                        Collections.sort(mPerformanceEntityArrayList, new ZHICollectGrouperPerformanceCompare.ZHICollectGrouperPerformanceCompareBySettleNumDESC());
//                    }
//
//                    mZHILevelDetailCollectGrouperB1.notifyDataSetChanged();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
            });

            ll_collected_rate_bg.setOnClickListener(v -> {
                try {
                    if (mPerformanceEntityArrayList == null) {
                        return;
                    }

                    if (mIsSortCollectedRateDes) {
                        mIsSortCollectedRateDes = false;
                        Collections.sort(mPerformanceEntityArrayList, new ZHICollectGrouperPerformanceCompare.ZHICollectGrouperPerformanceCompareBySettleNumASC());
                    } else {
                        mIsSortCollectedRateDes = true;
                        Collections.sort(mPerformanceEntityArrayList, new ZHICollectGrouperPerformanceCompare.ZHICollectGrouperPerformanceCompareBySettleNumDESC());
                    }

                    mZHILevelDetailCollectGrouperB1.notifyDataSetChanged();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            iv_filter.setOnClickListener(v -> {

            });

            TextViewUtil.setUnderLine(tv_detail_click);
            ll_detail_title_bg.setOnClickListener(v -> ZHIProjectRouter.toPerformanceGroupDetail());

            initEvent();
            initData(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onGetErrorInfo() {
        srl_swipe_layout.setRefreshing(false);
    }

    @Override
    public void onGetGroupB1B2PerformanceList(ZHIRCollectGrouperPerformanceListEntity zhirCollectGrouperPerformanceListEntity, ZHISPerfermanceEntity zhisPerfermanceEntity) {
        try {
            srl_swipe_layout.setRefreshing(false);

            mTotalBonus = zhirCollectGrouperPerformanceListEntity.getTotalTodayBonusFormat();
            mTotalPenalty = zhirCollectGrouperPerformanceListEntity.getTotalTodayPenaltyFormat();
            mTotalActualBonus = zhirCollectGrouperPerformanceListEntity.getTotalTodayActualBonusFormat();

            tv_total_case_num.setText(zhirCollectGrouperPerformanceListEntity.getTotalTodayAssignNum());
            tv_total_task_num.setText(zhirCollectGrouperPerformanceListEntity.getTotalTodayTaskNum());
            tv_total_settled_num.setText(zhirCollectGrouperPerformanceListEntity.getTotalTodayRepayNum());
            tv_total_bouns.setText(zhirCollectGrouperPerformanceListEntity.getTotalTodayActualBonusFormat());

            tv_cumulative_bonus.setText(zhirCollectGrouperPerformanceListEntity.getTotalBonusFormat());

            mLeaderDataList = zhirCollectGrouperPerformanceListEntity.getLeaderDataList();
            mPerformanceEntityArrayList = zhirCollectGrouperPerformanceListEntity.getDataList();
            mZHILevelDetailCollectGrouperB1.setNewData(mPerformanceEntityArrayList);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onGetMemberB1B2PerfermanceList(ZHIRCollectMemberPerformanceListEntity zhirCollectMemberPerformanceListEntity, ZHISPerfermanceEntity zhisPerfermanceEntity) {
        try {
            srl_swipe_layout.setRefreshing(false);

            ///////////////////////
            ArrayList<ZHIRCollecterPerformanceEntity> zhirCollecterPerformanceListEntityDataList = zhirCollectMemberPerformanceListEntity.getDataList();
            ZHIDialogCollectMemberPerformance.showDataSource(getActivity(), zhisPerfermanceEntity.getSysLoginName(), zhirCollecterPerformanceListEntityDataList);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressLint({"InflateParams", "SetTextI18n"})
    public void initEvent() {
        srl_swipe_layout.setOnRefreshListener(() -> initData(false));

        mZHILevelDetailCollectGrouperB1.setEmptyView(getRefreshEmptyView());
        mZHILevelDetailCollectGrouperB1.setOnItemClickListener((adapter, view, position) -> {
            if (mPerformanceEntityArrayList != null && mPerformanceEntityArrayList.size() > position) {
                ZHIRCollecterPerformanceEntity zhirCustomerPerformanceEntity = mPerformanceEntityArrayList.get(position);
                ZHISPerfermanceEntity zhisPerfermanceEntity = getUserCaseSearchParams(zhirCustomerPerformanceEntity);
                assert mPresenter != null;
                mPresenter.getMemberB1B2PerfermanceList(getActivity(), zhisPerfermanceEntity, true);
            }
        });

        rc_detail_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
        rc_detail_list.setAdapter(mZHILevelDetailCollectGrouperB1);
    }

    public void initData(boolean showDialog) {
        ZHISPerfermanceEntity zhisPerfermanceEntity = getCaseSearchParams();
        assert mPresenter != null;
        mPresenter.getGroupB1B2PerformanceList(getActivity(), zhisPerfermanceEntity, showDialog);
    }

    public void refreshData() {
        initData(true);
    }

    private ZHISPerfermanceEntity getCaseSearchParams() {
        ZHISPerfermanceEntity zhisPerfermanceEntity = new ZHISPerfermanceEntity();
        zhisPerfermanceEntity.setLeven(ZHIMainProcessHelper.getInstance().getLoginEntity().getDefaultLevenApiFormat());
        zhisPerfermanceEntity.setLeaderUserId(ZHIMainProcessHelper.getInstance().getLoginEntity().getUserId());
        return zhisPerfermanceEntity;
    }

    private ZHISPerfermanceEntity getUserCaseSearchParams(ZHIRCollecterPerformanceEntity zhirCustomerPerformanceEntity) {
        ZHISPerfermanceEntity zhisPerfermanceEntity = new ZHISPerfermanceEntity();
        zhisPerfermanceEntity.setSysLoginName(zhirCustomerPerformanceEntity.getSysLoginName());
        zhisPerfermanceEntity.setLeven(ZHIMainProcessHelper.getInstance().getLoginEntity().getDefaultLevenApiFormat());
        return zhisPerfermanceEntity;
    }

    private View getRefreshEmptyView() {
        View emptyView = getLayoutInflater().inflate(R.layout.view_empty_no_data, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setOnClickListener(v -> initData(true));
        return emptyView;
    }

    private void showBonusDialog() {
        ZHIDialogMessage.showDialog(getActivity(), null, new SpannableStringBuilder(""), true, null);
    }
}
